<template>
    <div class="views-goupiao-web-add">
        <div>
            <e-container>
                <el-card class="box-card">
                    <template #header>
                        <div class="clearfix">
                            <span class="title"> 添加购票 </span>
                        </div>
                    </template>

                    <el-form :model="form" ref="formModel" label-width="140px" status-icon validate-on-rule-change>
                        <el-form-item v-if="isRead" label="电影编号" prop="dianyingbianhao"> {{ form.dianyingbianhao }} </el-form-item>

                        <el-form-item v-if="isRead" label="电影名称" prop="dianyingmingcheng"> {{ form.dianyingmingcheng }} </el-form-item>

                        <el-form-item v-if="isRead" label="分类" prop="fenlei">
                            <e-select-view module="dianyingfenlei" :value="form.fenlei" select="id" show="fenleimingcheng"></e-select-view>
                        </el-form-item>

                        <el-form-item v-if="isRead" label="电影类型" prop="dianyingleixing"> {{ form.dianyingleixing }} </el-form-item>

                        <el-form-item v-if="isRead" label="影票价格" prop="yingpiaojiage" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]">
                            {{ form.yingpiaojiage }}
                        </el-form-item>

                        <el-form-item v-if="isRead" label="播放厅" prop="bofangting">
                            <e-select-view module="bofangting" :value="form.bofangting" select="id" show="bofangtingmingcheng"></e-select-view>
                        </el-form-item>

                        <el-form-item v-if="isRead" label="票数" prop="piaoshu" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]">
                            {{ form.piaoshu }}
                        </el-form-item>

                        <el-form-item label="选座" prop="xuanzuo">
                            <v-seat-chat v-if="bofangting.zuowei" :is-mul="true" @change-item="changeItem" v-model="form.xuanzuo" :unavailable="xuanzuo"
                                         :map="bofangting.zuowei"></v-seat-chat>

                        </el-form-item>

                        <el-form-item
                            label="购买票数"
                            prop="goumaipiaoshu"
                            required
                            :rules="[{required:true, message:'请填写购买票数'}, {validator:rule.checkNumber, message:'输入一个有效数字'}, {validator:rule.checkMin, message:'最少为1', value:1}, {validator:rule.checkMax, message:'票数不足', value:readMap.piaoshu}]"
                        >
                            <el-input type="number" placeholder="输入购买票数" style="width: 450px" v-model.number="form.goumaipiaoshu" />
                        </el-form-item>

                        <el-form-item label="购买人" prop="goumairen"> <el-input v-model="form.goumairen" readonly style="width: 250px"></el-input> </el-form-item>

                        <el-form-item v-if="btnText">
                            <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                        </el-form-item>
                    </el-form></el-card
                >
            </e-container>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive ,computed ,watch} from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useGoupiaoCreateForm, canGoupiaoInsert } from "@/module";
    import VSeatChat from '@/components/seat-chat/seat-chat'


    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
    });


    const { form, readMap } = useGoupiaoCreateForm(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;
            canGoupiaoInsert(form).then(
                (res) => {
                    loading.value = false;
                    if (res.code == 0) {
                        emit("success", res.data);
                        if (props.isHouxu) {
                            ElMessage.success("添加成功");
                            router.go(-1);
                        }
                    } else {
                        ElMessageBox.alert(res.msg);
                    }
                },
                (err) => {
                    loading.value = false;
                    ElMessageBox.alert(err.message);
                }
            );
        });
    };

    const changeItem = (e)=>{
        console.log(e);
        form.goumaipiaoshu = e.length;
    };

    const xuanzuo = ref('');
    const bofangting = ref({});

    const getBofangting = async ()=>{
        var bofangting1 = readMap.bofangting;
        if(bofangting1){
            bofangting.value = await DB.name("bofangting").find(bofangting1);
            var ids = await DB.name("goupiao").where("paipianid" , readMap.id).where("zhuangtai" , "!=" , "已退票").column("xuanzuo");
            xuanzuo.value = ids.join(',');

        }
    };
    watch(()=>readMap ,getBofangting , {immediate:true,deep:true})

</script>

<style scoped lang="scss">
    .views-goupiao-web-add {
    }
</style>
